{% extends "basic.html" %}
{% block head %}
    {{ super() }}
    <script type="text/javascript">
        function loading_software() {
            $.ajax({
                url: '/interface/software/select_id_softname',
                type: "GET",
                success: function (result) {
                    $('#div_display_software').html('');
                    soft_objs = JSON.parse(result);
                    for (i = 0; i < soft_objs.length; i++) {
                        id = soft_objs[i]['id']
                        softname = soft_objs[i]['softname']
                        $('#div_display_software').prepend('<div class="checkbox">' +
                                '<label><input type="checkbox" name="software" value="' + id + '">' + softname + '</label></div>');
                    }
                }
            });
        }
        loading_software();

        $.ajax({
            url: '/interface/server/select_id_server_group',
            type: "GET",
            success: function (result) {
                server_groups = JSON.parse(result);
                for (i = 0; i < server_groups.length; i++) {
                    server_group = server_groups[i]
                    $('#div_display_server').prepend('<div class="checkbox">' +
                            '<label><input type="checkbox" name="server_group" value="' + server_group + '">' + server_group + '</label></div>');
                }
            }
        });

        $(document).ready(function () {
            $('#button_scp_software').click(function () {
                var softids = new Array();
                $("[name='software']:checked").each(function () {
                    softids.push($(this).val());
                });
                softids = JSON.stringify(softids);
                ip = $('#input_ip').val();
                password = $('#input_password').val();
                $.ajax({
                    url: '/interface/software/upload',
                    type: "POST",
                    data: {softids: softids, ip: ip, password: password},
                    success: function (result) {
                        $('#p_scp_software_result').html(result);
                    }
                });
            });

            $("#shangchuan").click(function () {
                $.ajax({
                    url: '/interface/tool/upload',
                    type: 'POST',
                    cache: false,
                    data: new FormData($('#uploadForm')[0]),
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        $("#p_upload_result").html('上传成功，文件位置为：<br />' + data);
                    }
                });
            });

            $('#button_add_software').click(function () {
                input_softname = $('#input_softname').val();
                input_softpath = $('#input_softpath').val();
                if (!input_softname || !input_softpath) {
                    alert('请输入显示名称和软件名称！');
                    return false;
                }
                $.ajax({
                    url: '/interface/software/add',
                    type: "POST",
                    data: {softname: input_softname, softpath: input_softpath},
                    success: function (result) {
                        loading_software();
                    }
                });
            });
        });
    </script>
{% endblock %}
{% block content %}
    <div class="container">
        <div class="col-md-6">
            <p class="text-info">软件上传：</p>
            <form id="uploadForm" role="form" class="form" enctype="multipart/form-data">
                <input type="file" name="file"/><br/>
                <button id="shangchuan" type="button" class="btn btn-default">点击上传
                </button>
            </form>
            <p class="text-success" id="p_upload_result"></p><br/>

            <p class="text-info">增加软件列表：</p>
            <input id="input_softname" type="text" class="form-control" placeholder="请输入显示名称"/>
            <input id="input_softpath" type="text" class="form-control" placeholder="请输入软件名称"/>
            <button id="button_add_software" type="button" class="btn btn-default">增加软件列表</button>
            <br /><br/>

            <p class="text-info">软件列表：</p>
            <div id="div_display_software">
            </div>
            <input id="input_ip" type="text" class="form-control" name="ip" placeholder="请输入ip"/>
            <input id="input_password" type="password" class="form-control" name="password"
                   placeholder="请输入密码"/>
            <button id="button_scp_software" type="button" class="btn btn-default">上传软件</button>
            <p class="text-info" id="p_scp_software_result"></p><br/>

            <p class="text-info">服务器组列表：</p>
            <div id="div_display_server">
            </div>
        </div>
    </div>
{% endblock %}